package com.he.fetch.test.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "USER")
public class User implements java.io.Serializable {

    private static final long serialVersionUID = -4441228518744012464L;

    @OneToMany(mappedBy = "backup")
    private Set<Account> backupAccounts = new HashSet<Account>();

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", unique = true, updatable = false, nullable = false)
    private Integer id;

    @OneToMany(mappedBy = "manager")
    private Set<Account> managedAccounts = new HashSet<Account>();

    @Column(name = "NAME", unique = true, nullable = false, length = 255)
    private String name;

    @Column(name = "PASSWORD", length = 255)
    private String password;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER") }, inverseJoinColumns = { @JoinColumn(name = "ROLE") })
    private Set<Role> roles = new HashSet<Role>();

    public boolean add(Role role) {
	return this.roles.add(role);
    }

    public boolean addBackupAccount(Account account) {
	boolean added = this.backupAccounts.add(account);
	if (added) {
	    account.setBackup(this);
	}
	return added;
    }

    public boolean addManagedAccount(Account account) {
	boolean added = this.managedAccounts.add(account);
	if (added) {
	    account.setManager(this);
	}
	return added;
    }

    public Set<Account> getBackupAccounts() {
	return this.backupAccounts;
    }

    public Integer getId() {
	return this.id;
    }

    public Set<Account> getManagedAccounts() {
	return this.managedAccounts;
    }

    public String getName() {
	return this.name;
    }

    public String getPassword() {
	return this.password;
    }

    public Set<Role> getRoles() {
	return this.roles;
    }

    public boolean remove(Role role) {
	return this.roles.remove(role);
    }

    public void setBackupAccounts(Set<Account> backupAccounts) {
	this.backupAccounts = backupAccounts;
    }

    public void setId(Integer id) {
	this.id = id;
    }

    public void setManagedAccounts(Set<Account> managedAccounts) {
	this.managedAccounts = managedAccounts;
    }

    public void setName(String name) {
	this.name = name;
    }

    public void setPassword(String password) {
	this.password = password;
    }

    public void setRoles(Set<Role> roles) {
	this.roles = roles;
    }

    @Override
    public String toString() {
	return "[id=" + this.id + ", name=" + this.name + ", roles="
		+ this.roles + "]";
    }

}
